package cn.fz.learn02.controller;

import cn.fz.learn02.domain.pojo.Student;
import cn.fz.learn02.domain.dto.StudentDTO;
import cn.fz.learn02.domain.vo.Result;
import cn.fz.learn02.service.IStudentService;
import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "学生模块管理")
@RestController
@RequestMapping("/student")
@Slf4j
public class StudentController {
    @Autowired
    private IStudentService studentService;
    @GetMapping("{id}")
    @Operation(summary = "查询单个学生")
    public Result<Student> detail(@PathVariable("id") Integer id){
        log.info("查询单个学生");
        return Result.success(studentService.getById(id));
    }


    @GetMapping("/findAll")
    @Operation(summary = "查询所有学生")
    public Result<List<Student>> findAll(){
        log.info("查询所有学生信息");
        List<Student> list = studentService.list();
        return Result.success(list);
    }

    @PostMapping
    @Operation(summary = "添加学生信息")
    public Result<String> add(@RequestBody @Valid StudentDTO studentDTO){
        log.info(studentDTO.toString());
        Student student = BeanUtil.copyProperties(studentDTO, Student.class);
        boolean save = studentService.save(student);
        return save?Result.success():Result.error("添加失败");
    }
}
